package com.ruoyi.cases.utils.excel;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.utils.bean.BeanUtils;
import lombok.experimental.UtilityClass;

/**
 * HexBeanUtils class
 *
 * @author xuning
 * @date 2018/12/18 12:50
 */
@UtilityClass
public class HexBeanUtils extends BeanUtils {

    /**
     * 属性拷贝
     *
     * @param dest 目标
     * @param orig 源
     */
    public void copyProperties(final Object dest, final Object orig) {
            BeanUtils.copyProperties(dest, orig);
    }

    /**
     * 查找字段
     *
     * @param clazz 类
     * @return 字段
     */
    public List<Field> getClassField(Class<?> clazz) {
        List<Field> fieldList = new ArrayList<>();
        Class tempClass = clazz;
        //当父类为null的时候说明到达了最上层的父类(Object类).
        while (tempClass != null) {
            Field[] declaredFields = tempClass.getDeclaredFields();
            for (Field declaredField : declaredFields) {
                if (!Modifier.isStatic(declaredField.getModifiers())) {
                    fieldList.add(declaredField);
                    declaredField.setAccessible(true);
                }
            }
            //得到父类,然后赋给自己
            tempClass = tempClass.getSuperclass();
        }
        return fieldList;
    }

    /**
     * 实体类转map
     *
     * @param object 入参
     * @return
     */
    public static Map<String, Object> object2Map(Object object) {
        Map<String, Object> result = new HashMap<>();
        //获得类的的属性名 数组
        Field[] fields = object.getClass().getDeclaredFields();
        try {
            for (Field field : fields) {
                field.setAccessible(true);
                String name = field.getName();
                result.put(name, field.get(object));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

}
